package com.qiwenshare.file.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiwenshare.file.api.IDecryptFileService;
import com.qiwenshare.file.component.FileDealComp;
import com.qiwenshare.file.domain.DecryptFile;
import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.file_encryption.SM4FileEncryption;
import com.qiwenshare.file.file_encryption.Utils;
import com.qiwenshare.file.mapper.DecryptFileMapper;
import com.qiwenshare.file.mapper.FileMapper;
import com.qiwenshare.file.mapper.UserFileMapper;
import com.qiwenshare.file.vo.file.DecryptFileListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor=Exception.class)
public class DecryptFileService  extends ServiceImpl<DecryptFileMapper, DecryptFile> implements IDecryptFileService {
    @Resource
    UserFileMapper userFileMapper;
    @Resource
    DecryptFileMapper decryptFileMapper;
    @Resource
    FileDealComp fileDealComp;
    @Resource
    FileMapper fileMapper;


    @Override
    public void encryptUserFileByEncryptBatchNum(String encryptBatchNum) {


        LambdaQueryWrapper<UserFile> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userFileLambdaQueryWrapper.eq(UserFile::getEncryptBatchNum, encryptBatchNum);
        userFileMapper.delete(userFileLambdaQueryWrapper);



    }

    @Override
    public void decryptfile(String encryptBatchNum, String filePath, Long sessionUserId) {

        //进行文件解密操作
        //新增查询方式selectByEncryptBatchNum
        DecryptFile decryptfileTemp = decryptFileMapper.selectByEncryptBatchNum(encryptBatchNum);
        UserFile userfileTemp = userFileMapper.selectById(decryptfileTemp.getUserFileId());
        FileBean fileTemp = fileMapper.selectById(userfileTemp.getFileId());
        //System.out.println(fileTemp.getFileUrl());
        SM4FileEncryption sm4FileEncryption = new SM4FileEncryption();
        try {
            sm4FileEncryption.decrypt(Utils.transform("D:\\qiwen_file\\"+fileTemp.getFileUrl())+".sm4",Utils.transform("D:\\qiwen_file\\"+fileTemp.getFileUrl()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        LambdaUpdateWrapper<UserFile> userFileLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userFileLambdaUpdateWrapper.set(UserFile::getEncryptFlag, 0)
                .set(UserFile::getEncryptBatchNum, "")
                .eq(UserFile::getEncryptBatchNum, encryptBatchNum);
        userFileMapper.update(null, userFileLambdaUpdateWrapper);

        fileDealComp.restoreParentFilePath(filePath, sessionUserId);

        fileDealComp.deleteRepeatSubDirFile(filePath, sessionUserId);
        // TODO 如果被还原的文件已存在，暂未实现


        LambdaQueryWrapper<DecryptFile> decryptFileServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        decryptFileServiceLambdaQueryWrapper.eq(DecryptFile::getEncryptBatchNum, encryptBatchNum);
        decryptFileMapper.delete(decryptFileServiceLambdaQueryWrapper);
        //System.out.println(encryptBatchNum+"  "+filePath+"   "+sessionUserId);



    }

    @Override
    public List<DecryptFileListVo> selectDecryptFileList(Long userId) {
        return decryptFileMapper.selectDecryptFileList(userId);
    }
}
